
var datagrid = null;

$(function() {
	initMsgDatagird();
	$("#dateFrom").datepicker("option", "maxDate", new Date());
	$("#dateTo").datepicker("option", "maxDate", new Date());
	//verify/getCostDetail
	$("#costDetail_fancyA").fancybox({
		'autoScale'			: true,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});
	
	$("#addVerifyA").bind("click", function(){
		var rowVals = datagrid.getRowValue(2);
		if(rowVals.length == 1){
			var status = datagrid.getRowValueByName("status");
			if(status == "待审核"){
				var id = rowVals[0];
				seeDetail(id);
			}else{
				MZ.showMsg("已审核的数据不需要再审核，请选择<查看详细>!");
			}
		}else{
			MZ.showMsg("请选择一条报销数据进行审核!");
		}
	});
	
	$("#delVerifyA").bind("click", function(){
		var rowVals = datagrid.getRowValue(2);
		if(rowVals.length == 1){
			var status = datagrid.getRowValueByName("status");
			if(status == "待审核"){
				var id = rowVals[0];
				seeDetail(id);
			}else{
				MZ.showMsg("已审核的数据不需要再审核，请选择<查看详细>!");
			}
		}else{
			MZ.showMsg("请选择一条报销数据进行审核!");
		}
	});
	
	$("#searchA").bind("click", function(){
		searchVerify();
	});
	
	$("#resetA").bind("click", function(){
		$("#statusSel").val("");
		$("#dateFrom").val("");
		$("#dateTo").val("");
	});
	
});

//init message datagird
//get json data from server
function initMsgDatagird(){
	datagrid = $("#msgDatagird").flexigrid({
		url: "verify/getVerify",
		colModel : [
		    {display: "ID", name : "id", width : 90, sortable : true, hide: true},
		    {display: "BIZID", name : "bizId", width : 50, sortable : true, hide: true},
		    {display: "LEVEL", name : "level", width : 50, sortable : true, hide: true},
		    {display: "ROLE", name : "attribute", width : 50, sortable : true, hide: true, process:formatRole},
	        {display: "项目名称", name : "projectName", width : 150, sortable : true},
	        {display: "报销人", name : "refundUser", width : 90, sortable : true},
			{display: "报销时间", name : "gmtRefund", width : 90, sortable : true, process:formatTime},
			{display: "来自", name : "module", width : 90, sortable : true},
			{display: "审核状态", name : "status", width : 90, sortable : true, process:formatStatus},//process:formatStatus
			{display: "报销金额(合计)", name : "totalCost", width : 120, sortable : true, process:formatCost},
			{display: "提交人", name : "submitter", width : 90, sortable : true},
			{display: "操作", name : "id", width : 120, sortable : true, process:formatHref}
		]
    });
}

//funciton onSelectRow required
//update flexigrid onselectrow function
//if you need't use onselect row
//let this function blank to do nothing
function onSelectRow(tr){
}

function searchVerify(){
	var status = $("#statusSel").val();
	var dateFrom= $("#dateFrom").val();
	var dateTo = $("#dateTo").val();
	var params = [{name: "status", value: status},{name: "dateFrom", value: dateFrom},{name: "dateTo", value: dateTo}];
	$("#msgDatagird").flexOptions({newp: 1, params: params}).flexReload();
}

function formatTime(tdDiv, pid){
	return $(tdDiv).text().substr(0, 10);
}
//格式化STATUS显示
function formatStatus(tdDiv, pid){
	var status = $(tdDiv).text();
	if(status == -1){
		return "<font color='red'>未通过</font>";
	}else if(status == 1){
		return "<font color='blue'>待审核</font>";
	}else if(status == 2){
		return "<font color='green'>通过</font>";
	}else if(status == 0){
		return "<font color='purple'>待审核</font>";
	}else{
		return "<font color=''>审核取消</font>";
	}
}
function formatCost(tdDiv, pid){
	var cost = $(tdDiv).text();
	if(cost == "0"){
		return "";
	}
	return MZ.toDecimal(cost / MZ.CURRENCY_PRECISION , MZ.CURRENCY_ROUND);
}
function formatHref(tdDiv, pid){
	return "<a title='报销数据详细...' href='javascript:seeDetail("+$(tdDiv).text()+")'><font color='blue'>查看详细</font></a>";
}
function formatRole(tdDiv, pid){
	return eval( "(" + $(tdDiv).text() + ")" ).length;
}
function seeDetail(id){
	var bizId = $("#msgDatagird tr[id='"+id+"'] td[abbr='bizId'] div").text();
	var module = $("#msgDatagird tr[id='"+id+"'] td[abbr='module'] div").text();
	//alert(id+"----"+module);
	var url = "verify/getCostDetail";
	$("#costDetail_fancyA").attr("href", url+"/"+bizId+"/"+module+"/"+id);
	$("#costDetail_fancyA").trigger("click");
}
